class Hash

  # Sintax sugar for merge Hash
  def <<(hash)
    self.merge! hash
  end
end

module MenuSystem

  # Set roles of current user
  def current_roles
    @current_roles = [:guest]
    if logged_in?
      @current_roles = [:member]
      User.find(session[:user_id]).roles.each{|r| @current_roles << r.name.to_sym}
    end
    @current_roles
  end

  # Set current role's menu
  def current_menu
    @current_menu = Hash.new
    current_roles.each{|role| @current_menu << menu[role] unless menu[role].nil?}
    @current_menu << menu[:all] unless menu[:all].nil?
    @current_menu
  end

  def self.included(recipient)
    recipient.send :class_inheritable_hash, :menu
    recipient.send :menu=, {}
    recipient.send :before_filter, :current_roles, :current_menu
    recipient.extend( MenuRoleClassMethods )
  end

  module MenuRoleClassMethods
    def menu_for(role, hash_menu_role)
      menu[role] ||= Hash.new
      menu[role] << hash_menu_role
    end
  end

end

